package com.changeover.gwt.client.view.impl;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

public class CustomerStatusTab extends Composite {

    private VerticalPanel statusPanel = new VerticalPanel();
    private DateBox remainderDateBox = new DateBox();
    private ListBox statusCombo = new ListBox();
    private TextArea commentTextArea = new TextArea();
    private FlexTable flexTable = new FlexTable();
    private ListBox standartCommentCombo = new ListBox();
    private ListBox actionCombo = new ListBox();
    
    private Button actionSaveButton;
    
    private ClickHandler addRowClick = new ClickHandler() {
        @Override
        public void onClick(ClickEvent arg0) {
            final int numRows = flexTable.getRowCount();
            flexTable.setWidget(numRows, 0, new Label(getStatus()));
            flexTable.setWidget(numRows, 1, new Label(getAction()));
            flexTable.setWidget(numRows, 2, new Label(getReminder()));
            flexTable.setWidget(numRows, 3, new Label(getComment()));
            flexTable.setWidget(numRows, 4, 
                new Button("Remove", new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        flexTable.removeRow(flexTable.getCellForEvent(event).getRowIndex());
                    }})
                );
        }
    };

    public String getStatus() {
        return statusCombo.getItemText(statusCombo.getSelectedIndex());
    }

    public String getAction() {
        return actionCombo.getItemText(actionCombo.getSelectedIndex());
    }
    
    public String getReminder() {
        if (remainderDateBox.getValue()==null) return "";
        DateTimeFormat fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
        return fmt.format(remainderDateBox.getValue());
    }

    public String getComment() {
        return commentTextArea.getText();
    }

    public CustomerStatusTab() {

        statusPanel.setSize("680px", "189px");

        CaptionPanel actionWrapper = new CaptionPanel("Action");
        statusPanel.add(actionWrapper);
        actionWrapper.setWidth("700px");

        LayoutPanel actionLayoutPanel = new LayoutPanel();
        actionWrapper.setContentWidget(actionLayoutPanel);
        actionLayoutPanel.setSize("707px", "200px");

        Label lblStatus = new Label("Status");
        actionLayoutPanel.add(lblStatus);
        actionLayoutPanel.setWidgetLeftWidth(lblStatus, 12.0, Unit.PX, 44.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(lblStatus, 120.0, Unit.PX, 18.0, Unit.PX);

        statusCombo.addItem("correspondence started", "1");
        statusCombo.addItem("not responding", "2");
        actionLayoutPanel.add(statusCombo);
        actionLayoutPanel.setWidgetLeftWidth(statusCombo, 12.0, Unit.PX, 129.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(statusCombo, 144.0, Unit.PX, 22.0, Unit.PX);

        Label lblSetReminder = new Label("Reminder");
        actionLayoutPanel.add(lblSetReminder);
        actionLayoutPanel.setWidgetLeftWidth(lblSetReminder, 12.0, Unit.PX, 91.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(lblSetReminder, 66.0, Unit.PX, 18.0, Unit.PX);

        remainderDateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
        actionLayoutPanel.add(remainderDateBox);
        actionLayoutPanel.setWidgetLeftWidth(remainderDateBox, 10.0, Unit.PX, 129.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(remainderDateBox, 90.0, Unit.PX, 23.0, Unit.PX);

        actionSaveButton = new Button("Save", addRowClick);
        actionLayoutPanel.add(actionSaveButton);
        actionLayoutPanel.setWidgetLeftWidth(actionSaveButton, 12.0, Unit.PX, 129.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(actionSaveButton, 172.0, Unit.PX, 22.0, Unit.PX);
        
        Label lblAction = new Label("Action");
        actionLayoutPanel.add(lblAction);
        actionLayoutPanel.setWidgetLeftWidth(lblAction, 12.0, Unit.PX, 65.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(lblAction, 14.0, Unit.PX, 18.0, Unit.PX);
        
        actionCombo.addItem("action 1");
        actionCombo.addItem("action 2");
        actionCombo.addItem("action 3");
        actionLayoutPanel.add(actionCombo);
        actionLayoutPanel.setWidgetLeftWidth(actionCombo, 12.0, Unit.PX, 125.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(actionCombo, 38.0, Unit.PX, 22.0, Unit.PX);
        
        Label lblStandartComment = new Label("Standart comment");
        actionLayoutPanel.add(lblStandartComment);
        actionLayoutPanel.setWidgetLeftWidth(lblStandartComment, 148.0, Unit.PX, 129.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(lblStandartComment, 14.0, Unit.PX, 18.0, Unit.PX);

        standartCommentCombo.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {
                commentTextArea.setText("");
                commentTextArea.setText(standartCommentCombo.getItemText(standartCommentCombo.getSelectedIndex()));
            }
        });
        standartCommentCombo.addItem("Standart comment 1");
        standartCommentCombo.addItem("Standart comment 2");
        standartCommentCombo.addItem("Standart comment 3");
        standartCommentCombo.addItem("Standart comment 4");
        actionLayoutPanel.add(standartCommentCombo);
        actionLayoutPanel.setWidgetLeftWidth(standartCommentCombo, 283.0, Unit.PX, 400.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(standartCommentCombo, 9.0, Unit.PX, 22.0, Unit.PX);
        
        CaptionPanel cptnpnlNewPanel = new CaptionPanel("Comment");
        actionLayoutPanel.add(cptnpnlNewPanel);
        actionLayoutPanel.setWidgetLeftWidth(cptnpnlNewPanel, 145.0, Unit.PX, 562.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(cptnpnlNewPanel, 38.0, Unit.PX, 156.0, Unit.PX);
        cptnpnlNewPanel.setContentWidget(commentTextArea);
        commentTextArea.setSize("522px", "3cm");

        CaptionPanel historyWrapper = new CaptionPanel("History");
        statusPanel.add(historyWrapper);
        historyWrapper.setSize("702px", "205px");

        flexTable.setWidth("100%");
        flexTable.setWidget(0, 0, new Label("Status"));
        flexTable.setWidget(0, 1, new Label("Action"));
        flexTable.setWidget(0, 2, new Label("Reminder"));
        flexTable.setWidget(0, 3, new Label("Comment"));
        historyWrapper.add(flexTable);

        initWidget(statusPanel);

    }
}
